package com.atguigu.ssyx.activity.controller;

import com.atguigu.ssyx.activity.service.ICouponInfoService;
import com.atguigu.ssyx.common.result.Result;
import com.atguigu.ssyx.model.activity.CouponInfo;
import com.atguigu.ssyx.model.sys.Ware;
import com.atguigu.ssyx.vo.activity.CouponRuleVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.checkerframework.checker.units.qual.C;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 优惠券信息 前端控制器
 * </p>
 *
 * @author author
 * @since 2025-09-16
 */

//@CrossOrigin
@Api(tags = "优惠卷管理")
@RestController
@RequestMapping("/admin/activity/couponInfo")
@RequiredArgsConstructor
public class CouponInfoController {
    private final ICouponInfoService couponInfoService;

    /*url: `${api_name}/${page}/${limit}`,
    method: 'get'*/

    @ApiOperation("分页查询优惠卷列表")
    @GetMapping("{page}/{limit}")
    public Result list(@PathVariable Long page,
                       @PathVariable Long limit) {

        //1.创建分页对象
        Page<CouponInfo> p = new Page<>(page, limit);

        //2.构造条件
        LambdaQueryWrapper<CouponInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(CouponInfo::getId);
        //3.直接查询
        Page<CouponInfo> couponInfoPage = couponInfoService.page(p, wrapper);
        for (CouponInfo couponInfo : couponInfoPage.getRecords()) {
            //4.设置优惠卷类型描述
            couponInfo.setCouponTypeString(couponInfo.getCouponType().getComment());
            if (couponInfo.getRangeType() != null) {
                //5.设置优惠卷范围描述
                couponInfo.setRangeTypeString(couponInfo.getRangeType().getComment());
            }
        }
        //6.返回结果
        return Result.ok(p);
    }


/*    url: `${api_name}/get/${id}`,
    method: 'get'*/

    @ApiOperation("根据id查询优惠卷信息")

    @GetMapping("get/{id}")
    public Result get(@PathVariable Long id) {
        CouponInfo couponInfo = couponInfoService.getById(id);
        //设置优惠卷类型描述
        couponInfo.setCouponTypeString(couponInfo.getCouponType().getComment());
        if (couponInfo.getRangeType() != null) {
            //设置优惠卷范围描述
            couponInfo.setRangeTypeString(couponInfo.getRangeType().getComment());
        }
        return Result.ok(couponInfo);
    }

    /*    url: `${api_name}/save`,
        method: 'post',
        data: role*/
    @ApiOperation("保存优惠卷")
    @PostMapping("/save")
    public Result save(@RequestBody CouponInfo couponInfo) {
        couponInfoService.save(couponInfo);
        return Result.ok(null);
    }

    /* url: `${api_name}/update`,
     method: 'put',
     data: role
     */
    @ApiOperation("修改优惠卷信息")
    @PutMapping("/update")
    public Result update(@RequestBody CouponInfo couponInfo) {
        couponInfoService.updateById(couponInfo);
        return Result.ok(null);
    }

    /*    url: `${api_name}/remove/${id}`,
        method: 'delete'*/
    @ApiOperation("删除优惠卷信息")
    @DeleteMapping("/remove/{id}")
    public Result remove(@PathVariable Long id) {
        couponInfoService.removeById(id);
        return Result.ok(null);
    }

   /* url: `${api_name}/batchRemove`,
    method: 'delete',
    data: idList*/

    @ApiOperation("批量删除优惠卷")
    @DeleteMapping("/batchRemove")
    public Result batchRemove(@RequestBody List<Long> ids) {
        couponInfoService.removeByIds(ids);
        return Result.ok(null);
    }

   /* url: `${api_name}/findCouponRuleList/${id}`,
    method: 'get'*/

    @ApiOperation("根据优惠卷id查询优惠卷规则列表")
    @GetMapping("/findCouponRuleList/{id}")
    public Result findCouponRuleList(@PathVariable Long id) {

        Map<String,Object> map = couponInfoService.findCouponByIdRuleList(id);
        return Result.ok(map);

    }


   /* url: `${api_name}/saveCouponRule`,
    method: 'post',
    data: rule*/

    @ApiOperation("保存优惠卷规则")
    @PostMapping("/saveCouponRule")
    public Result saveCouponRule(@RequestBody CouponRuleVo  couponRuleVo) {
        couponInfoService.saveCouponRule(couponRuleVo);
        return Result.ok(null);
    }
}

